#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

def download(path, ul):
    import requests

    try:
        url = 'http://www.ncbi.nlm.nih.gov' + ul
        r = requests.get(url, timeout=180)
        with open("./pic/"+path+".gif", "wb") as code:
            code.write(r.content)
        print path + " has been downloaded."
        code.close()
    except:
        print path + " download exception."

def getURL(url):
    import urllib2
    from BeautifulSoup import BeautifulSoup

    content = urllib2.urlopen(url).read()
    soup = BeautifulSoup(content)
    return soup.find("img", attrs={"usemap": "#logo-imagemap"})["src"]

if __name__ == "__main__":
    import sys

    infile=open(sys.argv[1],"r")
    infile.readline()
    for line in infile:
        line=line.rstrip()
        splitline=line.split(",")
        journal_id=splitline[5]
        journal_id=journal_id[1:-1]
        url=splitline[12]
        filename="J_"+journal_id
        if url.startswith("http://www.ncbi.nlm.nih.gov/pmc/journals"):
            picurl=getURL(url)
            if picurl==None:
                print filename+" cannot get URL."
            else:
                download(filename,picurl)
    infile.close()